<?php

define('BASE_PATH', '/usr/local/htdocs');
require_once BASE_PATH.'/Library/phpQuery.php';
require_once BASE_PATH.'/model/TextDealer.php';

if( $argv[1] )
{
	$file = BASE_PATH.'/model/'.basename( $argv[1] ).'.php';
	if( is_file( $file ) )
	{
		require_once $file;
	}
	else
	{
		echo "need website parameter.\n";
		exit;
	}
}
else
{
	echo "need website parameter.\n";
	exit;
}

$result = array(
	'title'=>'',
	'content'=>'',
	'img'=>array(),
	'img_desc'=>'',
); 

function getStr($key, $value)
{
    global $result, $query;

	if( is_array( $query['title']['filter'] ) )
	{ 
		foreach( $query['title']['filter'] as $filter ) 
		{
			pq($filter)->remove();
		}
	}

	if( is_array( $query['title']['filter'] ) )
	{ 
		foreach( $query['title']['filter'] as $filter ) 
		{
			pq($filter)->remove();
		}
	}

    $str = pq($value)->text();
	return $str;
}

function getTitle($key, $value)
{
    global $result, $query;
	$str = getStr($key, $value);
	TextDealer::getInstance()->deal($str);
    $result['title'] = $str;
}

function getContent($key, $value)
{
    global $result, $query;
	pq($value)->find('img')->before('[picture]');
	pq($value)->find('img')->each('getImgSrc');
	$str = getStr($key, $value);
	TextDealer::getInstance()->deal($str);
	$result['content'] .= $str;
	if ( isset( $result['img'] )
		&& $result['img'] )
	{
		$texts = explode('[picture]', $str);
		$imgs = $result['img'];
		$result['img_desc'][] = array_shift( $texts ); 
		foreach( $imgs as $img )
		{
			$result['img_desc'][] = $img; 
			$result['img_desc'][] = array_shift( $texts ); 
		}

		$result['img_desc'] = array_diff( $result['img_desc'], array( '' ) ); 
	}
}

function getImgSrc( $node )
{
    global $result, $query; 
	$img = pq($node)->attr('src');
	if( $img )
	{
		$result['img'][] = $img;
	}	
}

function getImgDesc( $key, $value )
{
    global $result, $query; 
	pq($value)->find('img')->each('getImgSrc');
}

function getVideo( $key, $value )
{
    global $result, $query; 
	$str = pq($value)->find('embed')->attr('flashvars');
	if( $str )
	{
		if( preg_match( '/vid=[a-zA-Z0-9]*/', $str, $arr ) && $arr[1] )
		{
			$result['video']['vid'] = $arr[1];
		}

		if( preg_match( '/pic=.*\.jpg/', $str, $arr ) && $arr[1] )
		{
			$result['video']['pic'] = $arr[1];
		} 
	} 
}

function getNextUrl( $key, $value )
{
    global $result, $query; 
	$url = pq($value)->attr('href');

	if( $url )
	{
		if( !preg_match( '/^http/', $url ) )
		{
			$arr = explode(substr($url, 0, 5), $query['url']);
			$url = $arr[0].$url;
		}
		$query['next'] = $url; 
	} 
}


$query['history'] = array();
$query['next'] = $query['url'];
$maxPageNum = 0;

do
{ 
	$query['url'] = $query['next'];
	$query['next'] = '';
	$file = TextDealer::getInstance()->downloadUrl( $query['url'] );
	$doc = phpQuery::newDocumentFile( $file ); 
	if( !isset( $result['title'] )
		|| trim( $result['title'] ) == '')
	{
		phpQuery::each($doc[$query['title']['from']], 'getTitle', new CallbackParam, new CallbackParam);
	}
	//phpQuery::each($doc[$query['content']['from']], 'getImgDesc', new CallbackParam, new CallbackParam);
	phpQuery::each($doc[$query['content']['from']], 'getContent', new CallbackParam, new CallbackParam);
	//phpQuery::each($doc[$query['video']['from']], 'getVideo', new CallbackParam, new CallbackParam);
	phpQuery::each($doc[$query['multi']], 'getNextUrl', new CallbackParam, new CallbackParam); 

	if( $query['next'] 
		&&  !in_array( $query['next'], $query['history'] ) )
	{
		$query['history'][] = $query['url'];
		continue;
	}

	break;
}
while( $maxPageNum ++ < 20 );



print_r($result);








